<?php
// 公共文件
/**
 * 自定义输出
 * @param mixed $data
 * @param int $type [ 1-print_r 2-var_dump 3-var_export ]
 * @return void
 */
function d( $data, $type = 1 )
{
    // print_r
    if( $type == 1 )
    {
        print_r( $data );
    }

    // var_dump
    if( $type == 2 )
    {
        var_dump( $data );
    }

    // var_export
    if( $type == 3 )
    {
        var_export( $data );
    }

    exit;
}

/**
 * 加密函数
 * @param mixed $data 加密数据
 * @param string $key 密钥
 * @return string|void
 */
function jiami( $data, $key )
{
    //初始化向量
    $iv = config( 'user.iv' );
    
    //初始化加密类
    $encrypt = new SF\libaray\encrypt( $iv );

    //返回加密数据
    return $encrypt->encode( $data, $key );
}

/**
 * 解密函数
 * @param mixed $data 解密数据
 * @param string $key 密钥
 * @return string|void
 */
function jiemi( $data, $key )
{
    //初始化向量
    $iv = config( 'user.iv' );

    //初始化加密类
    $encrypt = new SF\libaray\encrypt( $iv );

    //返回解密数据
    return $encrypt->decode( $data, $key );
}

/**
 * 随机生成字符串
 * @param int $length 生成字符串长度
 * @param int $type 生成类型 [ 0-默认 1-数字 2-数字+小写字母+大写字母 ]
 * @param boolean $repeat 是否重复[ false ]
 */
function rand_str( $length, $type = 0, $repeat = true )
{
    $low_string = 'abcdefghijklmnopqrstuvwxyz';     //小写字母
    $power_string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';   //大写字母
    $number = '0123456789';                         //数字
    
    //种子
    $seed = '';
    switch ( $type )
    {
        case 1: $seed = $number;break;
        case 2: $seed = $number . $low_string . $power_string;break;
        default: $seed = $number . $low_string ;break;
    }
    $seed = str_split( $seed );
    
    //生成字符串
    $string = array();
    while ( count( $string ) < $length )
    {
        // 获取随机字符串
        $rand_index = mt_rand( 0, count( $seed ) - 1 );
        $rand_str = $seed[ $rand_index ];
        
        //判断是否重复
        if( ! $repeat )
        {
            if( in_array( $rand_str, $string ) )
            {
                continue;
            }
        }

        $string[] = $rand_str;
    }

    return implode( $string );
}

/**
 * 唯一id生成
 * @param string|int $in 输入数据
 * @param boolean $to_num 是否转数字
 * @param boolean|int $pad_up 输出长度
 * @param string $passKey 密钥
 * @return string|int
 */
function alpha_id( $in, $to_num = false, $pad_up = false, $passKey = null )
{
    $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if( $passKey !== null ) {

        for( $n = 0; $n < strlen( $index ); $n++ ) {
            $i[] = substr( $index, $n, 1 );
        }

        $passhash = hash( 'sha256', $passKey );
        $passhash = ( strlen( $passhash ) < strlen( $index ) )
            ? hash( 'sha512', $passKey )
            : $passhash;

        for( $n = 0; $n < strlen( $index ); $n++ ) 
        {
            $p[] =  substr( $passhash, $n, 1 );
        }

        array_multisort( $p,  SORT_DESC, $i );
        $index = implode($i);
    }
    $base  = strlen( $index );
    if( $to_num ) 
    {
        $in  = strrev( $in );
        $out = 0;
        $len = strlen( $in ) - 1;
        for( $t = 0; $t <= $len; $t++ ) 
        {
            $bcpow = bcpow( $base, $len - $t );
            $out   = $out + strpos( $index, substr( $in, $t, 1 ) ) * $bcpow;
        }

        if ( is_numeric( $pad_up ) )
        {
            $pad_up--;
            if( $pad_up > 0 )
            {
                $out -= pow( $base, $pad_up );
            }
        }
        $out = sprintf( '%F', $out );
        $out = substr( $out, 0, strpos( $out, '.' ) );
    }else{
        if( is_numeric( $pad_up ) )
        {
            $pad_up--;
            if( $pad_up > 0 )
            {
                $in += pow( $base, $pad_up );
            }
        }

        $out = "";
        for( $t = floor( log( $in, $base ) ); $t >= 0; $t-- )
        {
            $bcp = bcpow( $base, $t );
            $a   = floor( $in / $bcp ) % $base;
            $out = $out . substr( $index, $a, 1 );
            $in  = $in - ( $a * $bcp );
        }
        $out = strrev( $out ); // reverse
    }

    return $out;
}